# -*- coding: utf-8 -*-
##    This program is free software: you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation, either version 3 of the License, or
##    (at your option) any later version.
##
##    This program is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with this program.  If not, see <http://www.gnu.org/licenses/>
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from folder import *
class QtAppli(QApplication):
    "Fenêtre de l'application"
    def __init__(self, argv):
        QApplication.__init__(self, argv)
        self.m = MusicFile('C:\Users\wowi\Music\La rue ketanou')
        self.__mainWid=QMainWindow()
        self.__mainWid.setMinimumWidth(800)
        self.__mainWid.setMinimumHeight(800)
        self.__mainWid.setCentralWidget(QWidget(self.__mainWid))
        self.__mainWid.statusBar()
        self.__mainWid.setWindowTitle(self.trUtf8("Spydo Aquabeat Playlist Manager"))
        launch=QPushButton(self.trUtf8("Lancer la recherche de musique...", "Note: titre du bouton",), self.__mainWid.centralWidget())
        self.connect(launch, SIGNAL("clicked()"), self.__slotLaunch, )
        quit=QPushButton(self.trUtf8("Quitter", "Note: titre du bouton", ))
        self.connect(quit, SIGNAL("clicked()"), self.__mainWid, SLOT("close()"),)
        mainLayout=QVBoxLayout(self.__mainWid.centralWidget())
        mainLayout.addWidget(launch)
        mainLayout.addWidget(quit)

    def run(self):
        self.__mainWid.show()
        self.exec_()

    def __slotLaunch(self):
        dial=QDialog(self.__mainWid.centralWidget())
        dial.setModal(True)
        dial.setWindowTitle(self.trUtf8("Chargement en cours"))
        dial.show()
        self.m.findMusic()
        self.m.extractId3()
        self.m.affMusicList()
        self.m.sortListFromParam('id')
        dial.close()
        self.ListView()

    def __slotRandom(self):
        dial=QDialog(self.__mainWid.centralWidget())
        dial.setModal(True)
        dial.setWindowTitle(self.trUtf8("Mélange en cours"))
        dial.show()
        self.m.findMusic()
        self.m.extractId3()
        self.m.affMusicList()
        self.m.randomList()
        dial.close()
        self.ListView()

    def __slotSave(self):
        self.m.generateXml()
        dial=QMessageBox.information(self.__mainWid, self.trUtf8("Gestionnaire de Playlist"), self.trUtf8("Playlist sauvegardée"))

    def __slotInfo(self):
        dial=QMessageBox.information(self.__mainWid, self.trUtf8("Crédits"), self.trUtf8("<center><font size='+5'><font color='Blue'>Loïc Tosser M3 wowi_</font></font></center><center><font color='white'><a href=\"http://wowi.fr\">Site Web</a> <a href=\"https://twitter.com/#!/LoicTosserM3\">Twitter</a></font></center>"))
    def ListView(self):
        self.__mainWid.setCentralWidget(QWidget(self.__mainWid))
        table = QTableWidget(self.__mainWid.centralWidget())
        self.paramTable(table)
        random = QPushButton(self.trUtf8("Mélanger la liste..."))
        scan = QPushButton(self.trUtf8("Scanner le MP3"))
        save = QPushButton(self.trUtf8("Enregistrer la playlist"))
        info = QPushButton(self.trUtf8("Credits"))
        self.connect(scan, SIGNAL("clicked()"), self.__slotLaunch, )
        self.connect(random, SIGNAL("clicked()"), self.__slotRandom, )
        self.connect(save, SIGNAL("clicked()"), self.__slotSave, )
        self.connect(info, SIGNAL("clicked()"), self.__slotInfo, )
        mainLayout=QGridLayout(self.__mainWid.centralWidget())
        mainLayout.addWidget(table, 0, 0, 2, 2)
        mainLayout.addWidget(random, 2,0)
        mainLayout.addWidget(scan, 2,1)
        mainLayout.addWidget(save, 3,0)
        mainLayout.addWidget(info, 3,1)
        self.getData(table)
        self.__mainWid.show()
        
    def paramTable(self, table):
        table.setSortingEnabled(True)
        table.setColumnCount(7)
        table.setRowCount(len(self.m.musiclist))
        self.paramColumn(table)
        
    def paramColumn(self, table):
        a = QTableWidgetItem("id")
        table.setHorizontalHeaderItem(0, a)
        a = QTableWidgetItem("tracknumber")
        table.setHorizontalHeaderItem(1, a)
        a = QTableWidgetItem("title")
        table.setHorizontalHeaderItem(2, a)
        a = QTableWidgetItem("artist")
        table.setHorizontalHeaderItem(3, a)
        a = QTableWidgetItem("album")
        table.setHorizontalHeaderItem(4, a)
        a = QTableWidgetItem("genre")
        table.setHorizontalHeaderItem(5, a)
        b = QTableWidgetItem("path")
        b.setFlags(b.flags() & ~(QtCore.Qt.ItemIsDropEnabled | QtCore.Qt.ItemIsEditable))
        table.setHorizontalHeaderItem(6, b)

    def getData(self, table):
        i = 0
        for elem in self.m.musiclist:
            id = QTableWidgetItem(elem['id'])
            tracknumber = QTableWidgetItem(QString.fromUtf8(elem['tracknumber']))
            title = QTableWidgetItem(QString.fromUtf8(elem['title']))
            artiste = QTableWidgetItem(QString.fromUtf8(elem['artist']))
            album = QTableWidgetItem(QString.fromUtf8(elem['album']))
            genre = QTableWidgetItem(QString.fromUtf8(elem['genre']))
            path = QTableWidgetItem(QString.fromUtf8(elem['path']))
            table.setItem(i,0,id)
            table.setItem(i,1,tracknumber)
            table.setItem(i,2,title)
            table.setItem(i,3,artiste)
            table.setItem(i,4,album)
            table.setItem(i,5,genre)
            table.setItem(i,6,path)
            i +=1
        
if __name__ == "__main__":
    Appli=QtAppli(sys.argv,)
    Appli.run()
